package smtp;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailMessage extends MimeMessage {

	//private List<File> attachments; // keeps track of attachments in a list.  This may not be necessary
	private MimeMultipart multipart;
	private MimeBodyPart bodyText;

	private String format;

	public EmailMessage(Session session) {
		super(session);
		// initialize class variables
		// create the multipart object and add its first part, the body text
		// attachments = new LinkedList<File>();
		multipart = new MimeMultipart();
		bodyText = new MimeBodyPart();
		try {
			multipart.addBodyPart(bodyText);
			this.setContent(multipart);
		} catch (MessagingException e) {
			System.out.println("Error creating an Email Message.  This exception should not occur.");
			e.printStackTrace();
		}
	}

	public void addAttachment(File file) throws IOException{
		//attachments.add(file);
		MimeBodyPart attachment = new MimeBodyPart();
		try {
			attachment.attachFile(file);
			multipart.addBodyPart(attachment);
		} catch (MessagingException e) {
			System.out.println("EmailMessage: addAttachment: Error attaching file.");
			e.printStackTrace();
		}
	}

	/**
	 * Sets the corresponding fields in the email message.  If specified to be null, the corresponding field is removed.
	 * @param toAddresses
	 * @param fromAddress
	 * @param subject
	 * @param bodyText
	 * @throws MessagingException
	 * @throws IOException - IO problem in accessing one or more of the files
	 */
	public void fillForm(InternetAddress[] toAddresses, InternetAddress fromAddress, String subject, String bodyText, File[] attachmentFiles) throws MessagingException, IOException{
		this.setRecipients(RecipientType.TO, toAddresses);
		this.setFrom(fromAddress);
		this.setSubject(subject);
		this.bodyText.setText(bodyText);

		for(File f: attachmentFiles){
			addAttachment(f);
		}
	}

	/**
	 * There are some methods in the parent class that alter this message (specifically they alter the "content")
	 * in ways that do not align with the purpose of this class.  These methods are not disabled.  As a quick
	 * fix if some method is called which unintentionally overwrites the multipart content, this method can be
	 * used to recover it.
	 * 
	 * @throws MessagingException
	 */
	public void recoverMultipartContent() throws MessagingException{
		this.setContent(multipart);
	}

	@Override
	public void setText(String text){
		try {
			this.bodyText.setText(text);
		} catch (MessagingException e) {
			System.out.println("EmailMessage: setText: Error setting text.");
			e.printStackTrace();
		}
	}

	@Override
	public void setText(String text, String charset){
		try {
			this.bodyText.setText(text, charset);
		} catch (MessagingException e) {
			System.out.println("EmailMessage: setText: Error setting text.");
			e.printStackTrace();
		}
	}

	@Override
	public void setText(String text, String charset, String subtype) {
		try {
			this.bodyText.setText(text, charset, subtype);
		} catch (MessagingException e) {
			System.out.println("EmailMessage: setText: Error setting text.");
			e.printStackTrace();
		}
	}
<<<<<<< .mine
	public viod 
	
=======

	/**
	 * <date>
	 * <subject>
	 * <to>
	 * <from>
	 * @param format
	 * @return
	 */
	public void setToStringFormat(String format){
		this.format = format;
	}

	public String recipientsToString(){
		Address[] recipients = null;

		try {
			recipients = this.getAllRecipients();
		} catch (MessagingException e1) {
			System.out.println("Error accessing recipients for Email toString");
			e1.printStackTrace();
		}

		String to = "";
		for (Address r: recipients){
			to += "; " + r.toString();
		}

		return to.substring(2);
	}

	/**
	 * Formats toString based on the format if specified.  Else, returns a String of the subject and sender separated by a tab.
	 * 
	 */
	public String toString (){
		String returnString = null;

		try {
			if (format == null){
				returnString = this.getSubject() + "\t" + this.getSender().toString();
			} else {		
				returnString =  format.replaceAll("<date>", this.getSentDate().toString())
				.replaceAll("<subject>", this.getSubject())
				.replaceAll("<to>", recipientsToString())
				.replaceAll("<from>", this.getSender().toString());
			}
		} catch (MessagingException e) {
			System.out.println("Error accessing fields for Email toString");
			e.printStackTrace();
		}
		return returnString;
	}
>>>>>>> .r128
}
